package cn.itcast.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
      protected T model;
	@Override
	public T getModel() {
		return model;
	}
   public  BaseAction(){
	   //获取带有泛型的父类字节码对象；
	   Type genericSuperclass = this.getClass().getGenericSuperclass();
	   //获取第一个泛型的参数
	   ParameterizedType parameterizedType = (ParameterizedType)genericSuperclass;
	    Class<T> classModel = (Class<T>) parameterizedType.getActualTypeArguments()[0];
	            try {
	            	 //实例化泛型参数并给model赋值
					model = classModel.newInstance();
				}  catch (Exception e) {
					// TODO Auto-generated catch block
					System.out.println("模型驱动异常");
					e.printStackTrace();
				}
   }
// 属性驱动
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
 // 将值推送到值栈顶部
	protected void pushPageDataToValueStack(Page<T> pageData){
		Map<String ,Object > map = new HashMap<>();
		map.put("total", pageData.getTotalElements());
		map.put("rows",pageData.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
}
